package org.practice.dao;


import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.practice.model.Order;
import org.practice.model.User;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository(value="orderDao")
public class OrderDaoImpl extends HibernateTemplate implements OrderDao{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory=sessionFactory;
	}
	public void addOrder(Order order){
		this.save(order);
	}
	
	
	public List findOrderAddressByUser(User user){
		final String query="select o.address from Order as o where o.user.id=:userId order by o.id desc";
		final Integer userId=user.getId();
		return (List<?>)this.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				return session.createQuery(query).setInteger("userId", userId).list();
			}
		});
	}
}
